<?php

namespace app\api\controller\v1;

use app\common\controller\Api;
use think\Config;
use think\Db;
use think\Validate;

/**
 * 会员接口
 */
class Groupbargaining extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();

    }

    /**
     * 申请成为大团长
     */
    public function apply()
    {
        $user = $this->auth->getUser();
        $params = $this->request->post();
        if (empty($params['name'])){$this->error('姓名不存在');}
        if (empty($params['mobile'])){$this->error('手机号不存在');}
        $group_info = Db::name('shopro_group_identity')
            ->where('activity_id',0)
            ->where('activity_groupon_id',0)
            ->where('level',1)
            ->where('user_id',$user['id'])->find();

        if (!empty($group_info) && $group_info['status']==1){$this->error('当前用户已是大团长');}
        if (!empty($group_info) && $group_info['status']==0){$this->error('团长状态审核中');}
        if (!empty($group_info) && $group_info['status']==2){$this->error('团长状态冻结中');}
        $config = \addons\shopro\model\Config::where('name', 'order')->value('value');
        $config = json_decode($config, true);
        $insert = array(
            'user_id' => $user['id'],
            'level' => 1,
            'member_type'=>1,
            'commission_type'=>0,
            'commission_bili'=>$config['pintuan']['commission_bili'],
            'createtime' => time(),
            'name'=>$params['name'],
            'mobile'=>$params['mobile'],
            'ext'=>$params['ext']?:'',
        );
        $in = Db::name('shopro_group_identity')->insert($insert);
        if (!empty($in)){
            $this->success('申请团长成功，请等待审核');
        }else{
            $this->error('申请失败');
        }
    }
    /**
     * 申请成为小团长
     */
    public function mini_apply()
    {
        $user = $this->auth->getUser();
        $params = $this->request->post();
        if (empty($params['id'])){$this->error('id不存在');}
//        拼团详情信息
        $shopro_activity_groupon_log_info = Db::name('shopro_activity_groupon_log')->where('id',$params['id'])->find();
        if (empty($shopro_activity_groupon_log_info)){$this->error('拼团详情信息');}

        $groupon_id = $shopro_activity_groupon_log_info['groupon_id'];
//        $ee =  Db::name('shopro_group_identity')->where('level',1)->where('activity_groupon_id',$groupon_id)->find();
//        if ($ee['status']!=1){$this->error('大团长状态异常，无法申请成为小团长');}
        $group_info = Db::name('shopro_group_identity')->where('user_id',$user['id'])->where('activity_groupon_id',$groupon_id)->find();
        if (!empty($group_info)){$this->error('当前团用户已申请');}
//        团信息
        $activity_groupon_info = Db::name('shopro_activity_groupon')
            ->where('id',$groupon_id)
            ->find();

        if ($activity_groupon_info['expiretime']!=0 && $activity_groupon_info['expiretime']<time()){$this->error('当前活动已结束，无法申请');}
//        小团长个数
        $shopro_group_identity_info = Db::name('shopro_group_identity')
            ->where('activity_groupon_id',$activity_groupon_info['id'])
            ->where('level',2)
            ->select();

        $asdnfo = Db::name('shopro_activity_groupon_log')->where('groupon_id',$groupon_id)->where('user_id',$activity_groupon_info['user_id'])->find();
        if ($asdnfo['order_id']==0){
            if (count($shopro_group_identity_info)>=($activity_groupon_info['num'])){$this->error('当前可申请小团长个数超出成团人数');}
        }else{
            if (count($shopro_group_identity_info)>=($activity_groupon_info['num']-1)){$this->error('当前可申请小团长个数超出成团人数');}
        }
        $order_id = $shopro_activity_groupon_log_info['order_id'];
        $insert = array(
            'activity_id' => $shopro_activity_groupon_log_info['activity_id'],
            'activity_groupon_id' => $shopro_activity_groupon_log_info['groupon_id'],
            'parent_groupon_id'=>$groupon_id,
            'parent_user_id' => $activity_groupon_info['user_id'],
            'order_id' => $order_id,
            'user_id' => $user['id'],
            'member_type'=>2,
            'level' => 2,
            'createtime' => time()
        );
        $in = Db::name('shopro_group_identity')->insert($insert);
        if (!empty($in)){
            $this->success('申请小团长成功，请等待审核');
        }else{
            $this->error('申请失败');
        }
    }
    /**
     * 申请成为小小团长
     */
    public function minimini_apply()
    {
        $user = $this->auth->getUser();
        $params = $this->request->post();
        if (empty($params['id'])){$this->error('id不存在');}
        $config = \addons\shopro\model\Config::where('name', 'order')->value('value');
        $config = json_decode($config, true);
        if ($config['pintuan']['enable']==0){
            $this->error('暂未开启');
        }

//        拼团详情信息
        $shopro_activity_groupon_log_info = Db::name('shopro_activity_groupon_log')->where('id',$params['id'])->find();

        if (empty($shopro_activity_groupon_log_info)){$this->error('拼团详情信息');}

        $groupon_id = $shopro_activity_groupon_log_info['groupon_id'];
//        $ee =  Db::name('shopro_group_identity')->where('level',1)->where('activity_groupon_id',$groupon_id)->find();
//        if ($ee['status']!=1){$this->error('大团长状态异常，无法申请成为小团长');}
        $group_info = Db::name('shopro_group_identity')->where('user_id',$user['id'])->where('activity_groupon_id',$groupon_id)->find();
        if (!empty($group_info)){$this->error('当前团用户已申请');}
//        团信息
        $activity_groupon_info = Db::name('shopro_activity_groupon')
            ->where('id',$groupon_id)
            ->find();

        if ($activity_groupon_info['expiretime']!=0 && $activity_groupon_info['expiretime']<time()){$this->error('当前活动已结束，无法申请');}
//        小小团长个数
        $shopro_group_identity_info = Db::name('shopro_group_identity')
            ->where('activity_groupon_id',$activity_groupon_info['id'])
            ->where('level',3)
            ->select();

        $asdnfo = Db::name('shopro_activity_groupon_log')->where('groupon_id',$groupon_id)->where('user_id',$activity_groupon_info['user_id'])->find();
        if ($asdnfo['order_id']==0){
            if (count($shopro_group_identity_info)>=($activity_groupon_info['num'])){$this->error('当前可申请小小团长个数超出成团人数');}
        }else{
            if (count($shopro_group_identity_info)>=($activity_groupon_info['num']-1)){$this->error('当前可申请小小团长个数超出成团人数');}
        }
        $order_id = $shopro_activity_groupon_log_info['order_id'];


        $shangji = Db::name('shopro_group_identity')
            ->where('id',1)
            ->find();

        $insert = array(
            'activity_id' => $shopro_activity_groupon_log_info['activity_id'],
            'activity_groupon_id' => $params['order_top_groupon_id'],
            'parent_groupon_id'=>$groupon_id,
            'parent_user_id' => $activity_groupon_info['user_id'],
            'order_id' => $order_id,
            'user_id' => $user['id'],
            'member_type'=>2,
            'level' => 3,
            'createtime' => time()
        );
        $in = Db::name('shopro_group_identity')->insert($insert);
        if (!empty($in)){
            $this->success('申请小小团长成功，请等待审核');
        }else{
            $this->error('申请失败');
        }
    }
    /*大团长查看小团长申请列表*/
    public function mini_apply_list(){
        $user = $this->auth->getUser();
        $params = $this->request->post();
        if (empty($params['activity_groupon_id'])){$this->error('id不存在');}
        $list = Db::name('shopro_group_identity')
            ->alias('i')
            ->join('user u','u.id = i.user_id')
            ->where('i.activity_groupon_id',$params['activity_groupon_id'])
            ->where('i.parent_user_id',$user['id'])
            ->where('i.level',2)
            ->field('i.*,u.nickname,u.avatar,u.mobile')
            ->select();
        foreach ($list as &$v){
            $v['createtime'] = date('Y-m-d H:i:s', $v['createtime']);
            if ($v['mobile']){
                $v['mobile'] = substr_replace($v['mobile'],'****',3,4);
            }
            if ($v['status']==0){$v['status_text']='待同意';}
            if ($v['status']==1){$v['status_text']='已同意';}
        }
        unset($v);
        if (!empty($list)){
            $this->success('获取成功',$list);
        }else{
            $this->success('获取成功',[]);
        }
    }
    /*小团长查看小小团长申请列表*/
    public function minimini_apply_list(){
        $user = $this->auth->getUser();
        $params = $this->request->post();
        if (empty($params['activity_groupon_id'])){$this->error('id不存在');}
        $list = Db::name('shopro_group_identity')
            ->alias('i')
            ->join('user u','u.id = i.user_id')
            ->where('i.parent_groupon_id',$params['activity_groupon_id'])
            ->where('i.parent_user_id',$user['id'])
            ->where('i.level',3)
            ->field('i.*,u.nickname,u.avatar,u.mobile')
            ->select();
        foreach ($list as &$v){
            $v['createtime'] = date('Y-m-d H:i:s', $v['createtime']);
            if ($v['mobile']){
                $v['mobile'] = substr_replace($v['mobile'],'****',3,4);
            }
            if ($v['status']==0){$v['status_text']='待同意';}
            if ($v['status']==1){$v['status_text']='已同意';}
        }
        unset($v);
        if (!empty($list)){
            $this->success('获取成功',$list);
        }else{
            $this->success('获取成功',[]);
        }
    }
    /*大团长对小团长进行编辑*/
    public function update_mini(){
        $user = $this->auth->getUser();
        $params = $this->request->post();
        if (empty($params['id'])||(empty($params['commission_money'])&&empty($params['commission_bili']))){$this->error('参数缺失');}
        $info = Db::name('shopro_group_identity')->where('id',$params['id'])->find();
        if ($info['status']!=0){
            $this->error('当前用户不是待审核状态');
        }
        if ($info['level']!=2){
            $this->error('当前用户不是小团长');
        }
        if ($params['commission_type'] != $info['commission_type']){
            $this->error('当前分佣方式不符合');
        }
        if ($params['commission_type']==1){
            if ($params['commission_money']<=2){
                $this->error('当前分佣金额不能低于2元');
            }
        }
        if ($params['commission_type']==0){
            if ($params['commission_bili']<=2){
                $this->error('当前分佣比例不能低于2%');
            }
        }


        if ($info['parent_user_id']!=$user['id']){
            $this->error('当前用户不属于您的团');
        }
        $parent_info = Db::name('shopro_group_identity')
            ->where('activity_groupon_id',$info['activity_groupon_id'])
            ->where('user_id',$info['parent_user_id'])
            ->where('level',1)
            ->find();
        if ($parent_info['commission_type']==0){
            if ($parent_info['commission_bili']<=$params['commission_bili']){
                $this->error('分佣比例不正确');
            }
            Db::name('shopro_group_identity')->where('id', $params['id'])->update(['status'=>1,'commission_type'=>0,'commission_bili'=>$params['commission_bili']]);
        }
        if ($parent_info['commission_type']==1){
            if ($parent_info['commission_money']<=$params['commission_money']){
                $this->error('分佣金额不正确');
            }
            Db::name('shopro_group_identity')->where('id', $params['id'])->update(['status'=>1,'commission_type'=>1,'commission_money'=>$params['commission_money']]);
        }
        $this->success('通过成功');
    }

    /*小团长对小小团长进行编辑*/
    public function update_minimini(){
        $user = $this->auth->getUser();
        $params = $this->request->post();
        if (empty($params['id'])||(empty($params['commission_money'])&&empty($params['commission_bili']))){$this->error('参数缺失');}
        $info = Db::name('shopro_group_identity')->where('id',$params['id'])->find();
        if ($info['status']!=0){
            $this->error('当前用户不是待审核状态');
        }
        if ($params['commission_type'] != $info['commission_type']){
            $this->error('当前分佣方式不符合');
        }
        if ($params['commission_type']==1){
            if ($params['commission_money']<=1){
                $this->error('当前分佣金额不能低于1元');
            }
        }
        if ($params['commission_type']==0){
            if ($params['commission_bili']<=1){
                $this->error('当前分佣比例不能低于1%');
            }
        }


        if ($info['parent_user_id']!=$user['id']){
            $this->error('当前用户不属于您的团');
        }
        $parent_info = Db::name('shopro_group_identity')
            ->where('activity_groupon_id',$info['activity_groupon_id'])
            ->where('user_id',$info['parent_user_id'])
            ->where('level',2)
            ->find();
        if ($parent_info['commission_type']==0){
            if ($parent_info['commission_bili']<=$params['commission_bili']){
                $this->error('分佣比例不正确');
            }
            Db::name('shopro_group_identity')->where('id', $params['id'])->update(['status'=>1,'commission_type'=>0,'commission_bili'=>$params['commission_bili']]);
        }
        if ($parent_info['commission_type']==1){
            if ($parent_info['commission_money']<=$params['commission_money']){
                $this->error('分佣金额不正确');
            }
            Db::name('shopro_group_identity')->where('id', $params['id'])->update(['status'=>1,'commission_type'=>1,'commission_money'=>$params['commission_money']]);
        }
        $this->success('通过成功');
    }




}
